package org.ehe.erp.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.erp.domain.quality.ErpQualityItem;
import org.ehe.erp.domain.quality.bo.ErpQualityItemBo;
import org.ehe.erp.domain.quality.vo.ErpQualityItemVo;
import org.ehe.erp.service.quality.ErpBomQualityItemService;
import org.ehe.erp.service.quality.ErpQualityItemService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 质检项目
 * @author :zhangnn
 * @className :ErpQualityItemController
 * @description: TODO
 * @date 2025-09-10 16:42:00
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/quality/item")
public class ErpQualityItemController extends BaseController {
    private final ErpQualityItemService qualityItemService;

    /**
     * 查询质检项目列表（分页）
     */
    @SaCheckPermission("quality:item:list'")
    @GetMapping("/list")
    public TableDataInfo<ErpQualityItemVo> list(ErpQualityItemBo bo, PageQuery pageQuery) {
        return qualityItemService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询质检项目列表（不分页）
     */
    @SaCheckPermission("quality:item:list")
    @GetMapping("/listAll")
    public R<List<ErpQualityItemVo>> listAll(ErpQualityItemBo bo) {
        List<ErpQualityItemVo> list = qualityItemService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 获取质检项目详细信息
     */
    @SaCheckPermission("quality:item:query")
    @GetMapping("/{id}")
    public R<ErpQualityItemVo> getInfo(@PathVariable Long id) {
        ErpQualityItemVo vo = qualityItemService.queryById(id);
        return R.ok(vo);
    }

    /**
     * 新增质检项目
     */
    @SaCheckPermission("quality:item:add")
    @Log(title = "质检项目", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpQualityItemBo bo) {
        return toAjax(qualityItemService.insertByBo(bo));
    }

    /**
     * 修改质检项目
     */
    @SaCheckPermission("quality:item:edit")
    @Log(title = "质检项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpQualityItemBo bo) {
        return toAjax(qualityItemService.updateByBo(bo));
    }

    /**
     * 删除质检项目
     */
    @SaCheckPermission("quality:item:remove")
    @Log(title = "质检项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(qualityItemService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 校验质检项目编码是否唯一
     */
    @GetMapping("/checkItemCodeUnique")
    public R<Boolean> checkItemCodeUnique(@RequestParam String itemCode, @RequestParam(required = false) Long id) {
        return R.ok(qualityItemService.checkItemCodeUnique(itemCode, id));
    }

    /**
     * 根据编码查询质检项目
     */
    @GetMapping("/getByCode/{itemCode}")
    public R<ErpQualityItemVo> getByItemCode(@PathVariable String itemCode) {
        ErpQualityItem item = qualityItemService.getByItemCode(itemCode);
        if (item == null) {
            return R.fail("质检项目不存在");
        }
        ErpQualityItemVo vo = qualityItemService.queryById(item.getId());
        return R.ok(vo);
    }

    /**
     * 启用/禁用质检项目
     */
    @SaCheckPermission("quality:item:edit")
    @Log(title = "质检项目", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestParam Long id, @RequestParam Integer status) {
        ErpQualityItemBo bo = new ErpQualityItemBo();
        bo.setId(id);
        bo.setStatus(status);
        return toAjax(qualityItemService.updateByBo(bo));
    }
}
